/*
 * @Description: 特殊产品发货规则DS
 * @Version: 1.0
 * @Autor: c-jack.qian
 * @Date: 2020-12-28 20:03:25
 * @LastEditors: c-jack.qian
 * @LastEditTime: 2021-04-25 15:28:42
 */
import { DataSetSelection, FieldType } from 'choerodon-ui/pro/lib/data-set/enum';
import commonConfigStore from '@common/config/commonConfig';
import intl from 'utils/intl';
import { DataSet } from 'choerodon-ui/pro/lib';
const O2_OM = process.env.NODE_ENV === 'development' ? '/o2om-c' : commonConfigStore.O2_OM;

const isOption = new DataSet({
    selection: 'single' as DataSetSelection,
    data: [
        { text: intl.get('hzero.common.status.yes').d('是'), value: 1 },
        { text: intl.get('hzero.common.status.no').d('否'), value: 0 },
    ],
});

const specialProductDeliveryRulesDS = () => ({
    autoQuery: true,
    selection: 'multiple',
    primaryKey: 'id',
    transport: {
        read: config => {
            return {
                ...config,
                url: `${O2_OM}/v1/special-product-rules`,
                method: 'get',
            };
        },
        destroy: config => {
            return {
                ...config,
                url: `${O2_OM}/v1/special-product-rules`,
                method: 'delete',
            };
        },
    },
    fields: [
        {
            name: 'shippingRule',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.shippingRule').d('发货规则'),
            lookupCode: 'DJIOM.SPECIAL_SHIPPING_RULE',
        },
        {
            name: 'businessGroupMeaning',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.groupName').d('组别'),
        },
        {
            name: 'warehouseName',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.warehouseName').d('发货仓库'),
            required: true,
        },
        {
            name: 'productCode',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.productionCode').d('产品编码'),
        },
        {
            name: 'productName',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.productName').d('产品名称'),
        },
        {
            name: 'isLocalWarehouse',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.isLocalWarehouse').d('本地仓发货'),
            required: true,
            options: isOption,
            textField: 'text',
            valueField: 'value',
        },
        {
            name: 'createdUserName',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.createdUserName').d('创建人'),
        },
        {
            name: 'creationDate',
            type: FieldType.dateTime,
            label: intl.get('dji.otc.om.view.field.creationDate').d('创建时间'),
        },
    ],
    queryFields: [
        {
            label: intl.get('dji.otc.om.view.field.groupName').d('组别'),
            name: 'businessGroupCode',
            type: FieldType.string,
            lookupCode: 'DJICM_GROUP',
        },
        {
            label: intl.get('dji.otc.om.view.field.warehouseCode').d('发货仓库'),
            name: 'warehouseCode',
            type: FieldType.string,
            lookupCode: 'DJPCM_WAREHOUSE',
            textField: 'wareHouseName',
            valueField: 'wareHouseCode',
        },
        {
            name: 'productName',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.productName').d('产品'),
        },
    ],
});

const addSpecialProductDeliveryRulesDS = () => ({
    autoQuery: true,
    selection: 'multiple',
    primaryKey: 'id',
    transport: {
        create: config => {
            const {data} = config
            return {
                ...config,
                data: data[0],
                url: `${O2_OM}/v1/special-product-rules`,
                method: 'post',
            };
        },
    },
    fields: [
        {
            name: 'businessGroup',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.businessGroup').d('组别'),
            lookupCode: 'DJICM_GROUP',
            required: true,
        },
        {
            name: 'productName',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.productName').d('产品名称'),
            required: true,
        },
        {
            name: 'shippingRule',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.shippingRule').d('发货规则'),
            lookupCode: 'DJIOM.SPECIAL_SHIPPING_RULE',
            required: true,
            defaultValue: '111'
        },
        {
            label: intl.get('dji.otc.om.view.field.warehouseName').d('发货仓库'),
            name: 'warehouseObj',
            type: FieldType.object,
            lookupCode: 'DJPCM_WAREHOUSE',
            required: true,
            textField: 'wareHouseName',
            valueField: 'wareHouseCode',
            ignore: 'always'
        },
        {
            name: 'warehouseCode',
            bind: 'warehouseObj.wareHouseCode',
            type: FieldType.string,
        },
        {
            name: 'warehouseName',
            bind: 'warehouseObj.wareHouseName',
            type: FieldType.string,
        },
        {
            name: 'productCodeObj',
            type: FieldType.object,
            label: intl.get('dji.otc.om.view.field.productionCode').d('产品编码'),
            required: true,
            ignore: 'always',
            textField: 'skuCode',
            lovCode: 'O2PCM.QUERY_MASTER_SKU_LIST',
            lovQueryAxiosConfig: (code, lovConfig) => {
                console.log(code, lovConfig);
                const { queryUrl } = lovConfig && lovConfig.originData || {};
                return {
                    url: queryUrl,
                    method: 'post',
                };
            }
        },
        {
            name: 'productCode',
            bind: 'productCodeObj.skuCode',
            type: FieldType.string,
        },
        {
            name: 'productName',
            bind: 'productCodeObj.skuName',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.productName').d('产品名称'),
            required: true,
        },
        {
            name: 'isLocalWarehouse',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.isLocalWarehouse').d('本地仓发货'),
            required: true,
            options: isOption,
            textField: 'text',
            valueField: 'value',
            defaultValue: 0
        },
    ],
});


export { specialProductDeliveryRulesDS, addSpecialProductDeliveryRulesDS };
